﻿
<script type="text/javascript">
    $(function () {
        getButton("sqlquestion");
    });

    function ui_sqlquestion_init_list(toolbar) {
        $("#ui_sqlquestion_dg").datagrid({       //初始化datagrid
            url: "ashx/rm_sqlquestion.ashx?action=search",
            striped: true, rownumbers: true, pagination: true, pageSize: 20,
            idField: 'id',
            sortName: 'id',
            sortOrder: 'desc',
            pageList: [20, 40, 60, 80, 100],
            frozenColumns: [[
                { field: 'ck', checkbox: true },
                {
                    field: 'title', title: '试题名称', width: 300, sortable: true,
                    formatter: function (value, row, index) {
                        value = $('<div>' + value + '</div>').text();
                        return value.length > 100 ? '<span title="' + value + '">' + value.substring(0, 100) + ' </span> ...' : value;
                    }
                }
            ]],
            columns: [[
                {
                    field: 'refAnswer', title: '试题答案', width: 500, sortable: false,
                    formatter: function (value, row, index) {
                        return value.length > 100 ? '<span title="'+value+'">' + value.substring(0, 100) + ' </span> ...' : value;
                    }
                },
                { field: 'point', title: '试题分值', width: 80, sortable: true },
                { field: 'projectId', title: '项目编号', width: 80, sortable: true },
                { field: 'userId', title: '用户编号', width: 80, sortable: true },
                {
                    field: 'explain', title: '试题解析', width: 200, sortable: false,
                    formatter: function (value, row, index) {
                        value = $('<div>' + value + '</div>').text();
                        return value.length > 100 ? '<span title="' + value + '">' + value.substring(0, 100) + ' </span> ...' : value;
                    }
                }
            ]],
            toolbar: toolbar.length == 0 ? null : toolbar
            //toolbar: [{
            //    text: '添加',
            //    iconCls: 'icon-add',
            //    handler: function () { ui_sqlquestion_add(); }
            //}, {
            //    text: '修改',
            //    iconCls: 'icon-edit',
            //    handler: function () { ui_sqlquestion_edit(); }
            //}, '-', {
            //    text: '删除',
            //    iconCls: 'icon-remove',
            //    handler: function () { ui_sqlquestion_delete() }
            //}]
        });
    }
    function ui_sqlquestion_add() {
        $("<div/>").dialog({
            id: "ui_sqlquestion_add_dialog",
            href: "html/ui_sqlquestion_edit.html",
            title: "添加试题",
            iconCls: 'icon-add',
            height: 500,
            width: 800,
            modal: true,
            buttons: [{
                id: "ui_sqlquestion_add_btn",
                text: '添 加',
                title: "添加试题",
                iconCls: 'icon-add',
                handler: function () {
                    var projectId = $('#ui_sqlquestion_project').combobox('getValue');
                    if (!projectId || projectId.length == 0) {
                        alert("请选择项目！");
                        return;
                    }
                    var point = $('#ui_sqlquestion_point').numberbox('getValue');
                    if (!point || point.length == 0) {
                        alert("请输入试题分值！");
                        return;
                    }

                    var hastitle = UE.getEditor('ui_sqlquestion_title_editor').hasContents();
                    var title = UE.getEditor('ui_sqlquestion_title_editor').getContent();
                    if (!hastitle) {
                        $.show_alert("提示", "请输入试题名称！");
                        return;
                    }
                    if (title.length > 200) {
                        $.show_alert("提示", "试题名称不能超过100个字！");
                        return;
                    }
                    var answer = window.editor.getValue();
                    answer = $.trim(answer);
                    if (!answer || answer.length == 0) {
                        $.show_alert("提示", "请输入答案！");
                        return;
                    }
                    var explain = UE.getEditor('ui_sqlquestion_explain_editor').getContent();
                    
                    para = {};
                    para.action = 'add';
                    para.timespan = new Date().getTime();
                    para.title = title;
                    para.answer = answer;
                    para.explain = explain;
                    para.point = point;
                    para.projectId = projectId;
                    $.ajax({
                        url: "ashx/rm_sqlquestion.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                UE.getEditor('ui_sqlquestion_explain_editor').destroy();
                                UE.getEditor('ui_sqlquestion_title_editor').destroy();
                                $("#ui_sqlquestion_add_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "添加成功！");
                                $("#ui_sqlquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');

                            } else {
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });
                }
            }],
            onLoad: function () {                
                UE.getEditor('ui_sqlquestion_title_editor');
                UE.getEditor('ui_sqlquestion_explain_editor');
                var mime = 'text/x-sql';
                window.editor = CodeMirror.fromTextArea(document.getElementById('ui_sqlquestion_answer_editor'), {
                    mode: mime,
                    indentWithTabs: true,
                    smartIndent: true,
                    lineNumbers: true,
                    matchBrackets: true,
                    autofocus: true,

                    extraKeys: { "Alt-/": "autocomplete" },
                    viewportMargin: Infinity,
                    hintOptions: {
                        tables: {
                            t_student: { sno: null, sname: null, birthday: null },
                            t_group: { gno: null, gname: null, gmentor: null }
                        }
                    }
                });
                editor.setSize('auto', 'auto');
            },
            onClose: function () {
                UE.getEditor('ui_sqlquestion_explain_editor').destroy();
                UE.getEditor('ui_sqlquestion_title_editor').destroy();
                $("#ui_sqlquestion_add_dialog").dialog('destroy'); 
            }
        });
    }
    function ui_sqlquestion_edit() {
        var rows = $("#ui_sqlquestion_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要修改的按钮或者双击某个项目");
            return;
        }
        if (rows.length > 1) {
            $.show_warning("提示", "不支持批量修改");
            $("#ui_sqlquestion_dg").datagrid('clearSelections').datagrid('clearChecked');
            return;
        }
        var row = rows[0];
        $("<div/>").dialog({
            id: "ui_sqlquestion_edit_dialog",
            href: "html/ui_sqlquestion_edit.html",
            title: "修改项目",
            iconCls: 'icon-edit',
            height: 500,
            width: 800,
            modal: true,
            buttons: [{
                id: "ui_sqlquestion_edit_btn",
                iconCls: 'icon-edit',
                text: '修 改',
                handler: function () {
                    var id = $('#ui_sqlquestion_id').val();
                    var projectId = $('#ui_sqlquestion_project').combobox('getValue');
                    if (!projectId || projectId.length == 0) {
                        alert("请选择项目！");
                        return;
                    }
                    var point = $('#ui_sqlquestion_point').numberbox('getValue');
                    if (!point || point.length == 0) {
                        alert("请输入试题分值！");
                        return;
                    }

                    var hastitle = UE.getEditor('ui_sqlquestion_title_editor').hasContents();
                    var title = UE.getEditor('ui_sqlquestion_title_editor').getContent();
                    if (!hastitle) {
                        $.show_alert("提示", "请输入试题名称！");
                        return;
                    }
                    if (title.length > 100) {
                        $.show_alert("提示", "试题名称不能超过100个字！");
                        return;
                    }
                    var answer = window.editor.getValue();
                    answer = $.trim(answer);
                    if (!answer || answer.length == 0) {
                        $.show_alert("提示", "请输入答案！");
                        return;
                    }
                    var explain = UE.getEditor('ui_sqlquestion_explain_editor').getContent();

                    para = {};
                    para.action = 'edit';
                    para.timespan = new Date().getTime();
                    para.id = id;
                    para.title = title;
                    para.answer = answer;
                    para.explain = explain;
                    para.point = point;
                    para.projectId = projectId;
                    $.ajax({
                        url: "ashx/rm_sqlquestion.ashx",
                        data: para,
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                UE.getEditor('ui_sqlquestion_explain_editor').destroy();
                                UE.getEditor('ui_sqlquestion_title_editor').destroy();
                                $("#ui_sqlquestion_edit_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", "修改成功！");
                                $("#ui_sqlquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');

                            } else {
                                $.show_warning("提示", data.msg);
                            }
                        }
                    });
                }
            }],            
            onLoad: function () {
                $('#ui_sqlquestion_id').val(row.id);
                $('#ui_sqlquestion_project').combobox('setValue', row.projectId);
                $('#ui_sqlquestion_point').numberbox('setValue', row.point);
                $('#ui_sqlquestion_answer_editor').val(row.refAnswer);
                var ueTitle=UE.getEditor('ui_sqlquestion_title_editor');
                var ueExplain = UE.getEditor('ui_sqlquestion_explain_editor');
                var mime = 'text/x-sql';
                window.editor = CodeMirror.fromTextArea(document.getElementById('ui_sqlquestion_answer_editor'), {
                    mode: mime,
                    indentWithTabs: true,
                    smartIndent: true,
                    lineNumbers: true,
                    matchBrackets: true,
                    autofocus: true,

                    extraKeys: { "Alt-/": "autocomplete" },
                    viewportMargin: Infinity,
                    hintOptions: {
                        tables: {
                            t_student: { sno: null, sname: null, birthday: null },
                            t_group: { gno: null, gname: null, gmentor: null }
                        }
                    }
                });
                
                ueTitle.addListener("ready", function () {
                    ueTitle.setContent(row.title);
                    ueTitle.focus();
                });
                ueExplain.addListener("ready", function () {
                    ueExplain.setContent(row.explain);                    
                });
                
            },
            onClose: function () {
                UE.getEditor('ui_sqlquestion_explain_editor').destroy();
                UE.getEditor('ui_sqlquestion_title_editor').destroy();
                $("#ui_sqlquestion_edit_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
    function ui_sqlquestion_delete() {
        var rows = $("#ui_sqlquestion_dg").datagrid("getChecked");
        if (rows.length < 1) {
            $.show_warning("提示", "请先勾选要删除的按钮");
            return;
        }
        $.messager.confirm('提示', '确定删除勾选的这' + rows.length + '个按钮？', function (r) {
            if (r) {
                para = {};
                para.action = "delete";
                para.timespan = new Date().getTime();

                var ids = [];
                $.each(rows, function (i, row) {
                    ids.push(row.id);
                });
                para.ids = ids.join(',');
                $.ajax({
                    url: "ashx/rm_sqlquestion.ashx",
                    data: para,
                    type: "POST",
                    dataType: "json",
                    success: function (data) {
                        if (data.success) {
                            $.show_warning("提示", "删除成功！");
                            $("#ui_sqlquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                        } else {
                            $.show_warning("提示", data.msg);
                        }
                    }
                });
            }
        });
    }

    //上传导入数据
    function ui_sqlquestion_import() {

        $("<div/>").dialog({
            id: "ui_sqlquestion_import_dialog",
            href: "html/ui_sqlquestion_import.html",
            iconCls: "icon-filter",
            title: '数据导入',
            height: 220,
            width: 420,
            modal: true,
            buttons: [{
                id: "ui_sqlquestion_import_btn",
                text: '导入',
                iconCls: "icon-import",
                handler: function () {
                    var v = $("#ui_sqlquestion_import_file").val();
                    //判断是否选择图片  
                    if (v == null || v == "") {
                        //$.messager.alert('提示', '请选择要导入的文件!');
                        $("#ui_sqlquestion_import_file").focus();
                        return;
                    }
                    $('#ui_sqlquestion_import_btn').linkbutton('disable');
                    var options = {
                        type: "POST",
                        url: 'ashx/rm_sqlquestion.ashx',
                        data: { action: 'import', t: new Date().getTime() },
                        success: function (data) {                            
                            $('#ui_sqlquestion_importform_p').progressbar('setValue', 100);
                            var dataJson = JSON.parse(data);    //转成json格式
                            if (dataJson.success) {
                                $("#ui_sqlquestion_import_dialog").dialog('destroy');  //销毁dialog对象
                                $.show_warning("提示", dataJson.msg);
                                $("#ui_sqlquestion_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                            } else {
                                
                                $('#ui_sqlquestion_importform_msg').html($('<div/>').html(dataJson.msg).text());                                
                            }
                            $('#ui_sqlquestion_import_btn').linkbutton('enable');  //恢复按钮
                        }
                    };
                    // 将options传给ajaxForm  
                    ui_sqlquestion_start();
                    $('#ui_sqlquestion_importform').ajaxSubmit(options);                    
                }
            }],
            onLoad: function () {
            },
            onClose: function () {
                $("#ui_sqlquestion_import_dialog").dialog('destroy');  //销毁dialog对象
            }
        });
    }
</script>
<div id="ui_sqlquestion_layout" class="easyui-layout" data-options="fit:true,border:false">    
    <div data-options="region:'center',border:false">
        <table id="ui_sqlquestion_dg" data-options="fit:true,border:false">
        </table>
    </div>
</div>
